<template>
  <div class="login">
    <div class="left">
      <!-- 标题栏 -->
      <div class="herader">
        <img src="@/assets/img/login_logo.png" class="t1" alt />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>
      <!-- 表单 
        表单验证： 
          1：在el-form上绑定  model rules
          2：在el-form-item 绑定prop：验证项 的字段名
            rules:{
              prop值:[
              {required:true,message:'错误信息',trigger:"blur/change"},
              {min:最小长度,max:最大,message:'错误信息',trigger:"blur/change"}
              ]
            }
            全局表单验证
            1:el-form model rules ref
            2:调用el-form 的validate
              this.$refs.ref值.validate((result) =>{
                result: true:验证通过
                        false:验证失败
              })
      -->
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input placeholdert="请输入验证码" v-model="form.code" prefix-icon="el-icon-key"></el-input>
            </el-col>
            <el-col :span="8">
              <img
                class="code-img"
                style="width:100%;height:40px"
                :src="codeImgUrl"
                @click="changeCode"
                alt
              />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass" :label="true">
            我已阅读同意
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="login">登录</el-button>
          <div></div>
          <el-button type="primary" style="width:100%;margin-top:26px" @click="showRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <img src="@/assets/img/login.png" alt />

    <register ref="register"></register>
  </div>
</template>

<script>
// 组件套用组件 1：导入 2：注册 3：当标签使用
import { toLogin } from "@/api/login.js";
import register from "./register";
import { saveLocal,getLocal } from "@/utils/local.js";
export default {
  components: {
    register,
  },
  data() {
    return {
      form: {
        phone: "",
        password: "",
        code: "",
        isPass: false,
      },
      codeImgUrl: process.env.VUE_APP_URL + "/captcha?type=login", //图形验证码地址
      // 验证规则
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "change" },
          {
            min: 11,
            max: 11,
            message: "请输入11位手机号",
            trigger: "change",
          },
          {
            validator: (rule, value, callback) => {
              let _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (_reg.test(value)) {
                callback();
              } else {
                callback(new Error("请正确输入手机号"));
              }
            },
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 6,
            max: 12,
            message: "请输入6-12位密码",
            trigger: "change",
          },
        ],
        code: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "change",
          },
          {
            min: 4,
            max: 4,
            message: "请输入验证码",
            trigger: "change",
          },
        ],
        isPass: [
          {
            required: true,
            message: "请勾选协议",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    // if(已登录(有token)){
    //   跳转至layout this.$router.push('/layout')
    // }
    if (getLocal()) {
      this.$router.push("/layout");
    }
  },
  methods: {
    login() {
      this.$refs.form.validate((result) => {
        if (result) {
          toLogin(this.form).then((res) => {
            this.$message.success("登录成功");
            saveLocal( res.data.data.token);
            // 登录成功后跳转layout
            this.$router.push("/layout");
            window.console.log("登录返回信息", res);
          });
        } else {
          this.$message.error("验证失败");
        }
      });
    },

    // 注册
    showRegister() {
      // 父传子 1：在子组件标签定义一个ref 2：获取子组件this===this.$refs.ref值
      this.$refs.register.isShow = true;
    },
    //  刷新验证码
    changeCode() {
      this.codeImgUrl =
        process.env.VUE_APP_URL +
        "/captcha?type=login&ain=sda" +
        Math.random() * 1000;
    },
  },
};
</script>

<style lang='less'>
.login {
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  // 上下居中
  align-items: center;
  justify-content: space-around;
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 44px 42px;
    .herader {
      display: flex;
      align-items: center;
      .t1 {
        width: 25px;
      }
      .t2 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin-left: 13px;
        margin-right: 13px;
      }
      .t3 {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin-right: 13px;
      }
      .t4 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    // form
    form {
      margin-top: 31px;
    }
  }
}
</style>